import { cronJobLogMock } from './data/log'
import { isDateTimeInRange } from '../../utils'
let cronJobLogList = cronJobLogMock(400)
export default [
  {
    url: RegExp('/infra/cron-job-log/detail' + '.*'), // 获取详情
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getInfoById(params)
      }
    }
  },
  {
    url: RegExp('/infra/cron-job-log/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/infra/cron-job-log/export' + '.*'), // 导出
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByExport(params)
      }
    }
  }
]

const getInfoById = params => {
  const result = cronJobLogList.filter(item => {
    return item.id === parseInt(params.query.id)
  })
  if (result.length > 0) {
    return result[0]
  } else {
    return null
  }
}

const getListByPage = params => {
  let result = cronJobLogList
  result = result.filter(item => !item.deleted)
  if (params.query.jobId) {
    result = result.filter(item => item.jobId === parseInt(params.query.jobId))
  }
  if (params.query.status) {
    result = result.filter(item => item.status === parseInt(params.query.status))
  }
  if (params.query.handlerName) {
    result = result.filter(item => item.handlerName.indexOf(params.query.handlerName) !== -1)
  }
  if (params.query.beginTime && params.query.beginTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.beginTime[0], params.query.beginTime[1], item.beginTime))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getListByExport = params => {
  let result = cronJobLogList
  result = result.filter(item => !item.deleted)
  if (params.query.jobId) {
    result = result.filter(item => item.jobId === parseInt(params.query.jobId))
  }
  if (params.query.status) {
    result = result.filter(item => item.status === parseInt(params.query.status))
  }
  if (params.query.handlerName) {
    result = result.filter(item => item.handlerName.indexOf(params.query.handlerName) !== -1)
  }
  if (params.query.beginTime && params.query.beginTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.beginTime[0], params.query.beginTime[1], item.beginTime))
  }
  return result
}
